package com.leetcode.Offer;

public class JZ16 {
    public double myPow(double x, int n) {

        if (n == 0) {
            return 1;
        } else if (n < 0) {
            return 1 / (x * myPow(x, -n - 1));
        } else if (n % 2 == 1) {
            return x * myPow(x, n - 1);
        } else {
            return myPow(x * x, n / 2);
        }

    }

    public double myPow_2(double x, int n) {

        if (n == 0) {
            return 1;
        } else if (n < 0) {
            return 1 / myPow(x, n);
        } else if (n % 2 != 0) {
            return x * myPow(x, n - 1);
        } else {
            return myPow(x * x, n / 2);
        }

    }

    public static void main(String[] args) {
        System.out.println(new JZ16().myPow(2, -2147483648));
    }
}
/*如果n == 0，返回1
如果n < 0，最终结果为 1/x^{-n}1/x
−n

如果n为奇数，最终结果为 x * x ^ {n - 1}x∗x
n−1

如果n为偶数，最终结果为 x ^ {2*(n/2)}x
2∗(n/2)

https://leetcode-cn.com/problems/shu-zhi-de-zheng-shu-ci-fang-lcof/solution/jian-zhi-offer-16-shu-zhi-de-zheng-shu-c-rgqy/
* */